package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.enums.OrderTypeEnum;
import com.weixing.mall.provider.api.model.vo.AgentRpcVo;
import com.weixing.mall.provider.manager.AgentManager;
import com.weixing.mall.provider.model.enums.*;
import com.weixing.mall.provider.model.query.AfterSaleQuery;
import com.weixing.mall.provider.model.query.AgentOrderQuery;
import com.weixing.mall.provider.model.vo.FrontAgentOrderVo;
import com.weixing.mall.provider.model.vo.SalesAchiVo;
import com.weixing.mall.provider.model.vo.agent.AbnormalOrderStatisticVo;
import com.weixing.mall.provider.model.vo.agent.AgentSaleStatisticVo;
import com.weixing.mall.provider.model.vo.agent.UpPayStatisticVo;
import com.weixing.mall.provider.service.IAgentOrderService;
import com.weixing.mall.provider.service.IKefuOrderService;
import com.weixing.mall.provider.service.IOrderAgentService;
import com.weixing.mall.provider.util.AgentOrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客服APP-客服订单报表API
 *
 * @author siashan
 * @since v2.0
 **/
@Validated
@RestController
@RequestMapping("kefu/order/report")
public class KefuOrderReportController extends BaseController {

    @Autowired
    private AgentManager agentManager;
    @Autowired
    private IAgentOrderService agentOrderService;
    @Autowired
    private IOrderAgentService orderAgentService;
    @Autowired
    private IKefuOrderService kefuOrderService;


    /**
     * 销售报表--明细
     *
     * @param query 查询参数
     * @return 销售明细
     */
    @PostMapping("sales")
    public Result<IPage<FrontAgentOrderVo>> salesDetail(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        IPage<FrontAgentOrderVo> frontAgentOrderVoIPage = kefuOrderService.listOrderOfPage(initMpPage(), query);
        return ResultUtil.success(frontAgentOrderVoIPage);
    }


    /**
     * 销售报表--统计
     *
     * @apiNote 仅支持时间范围查询
     * @return 销售统计
     */
    @PostMapping("sales/statistics")
    public Result<AgentSaleStatisticVo> salesStatistics(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        AgentSaleStatisticVo relust = kefuOrderService.listOrderSalesStatistics(query);
        return ResultUtil.success(relust);
    }

    private void wrapSale(AgentOrderQuery query) {
        query.setAuditStatus(OrderAuditStateEnum.KEFU_AUDITING);
        query.setAuditStartTime(query.getStartTime());
        query.setAuditEndTime(query.getEndTime());
        query.setStartTime(null);
        query.setEndTime(null);
    }

//    /**
//     * 线上返利报表
//     *
//     * @apiNote 线上订单 & 正价订单 & 会员下单 & 客服审核通过 & 包含下级订单
//     *
//     * @param query 查询条件
//     * @return 返利报表
//     */
//    @PostMapping("profit")
//    public Result<IPage<FrontAgentOrderVo>> profitOrderDetail(@RequestBody AgentOrderQuery query) {
//        Long abid = AuthUtil.getMemberExt().getAbid();
//        query.setAgentIdForQueryAll(abid);
//        query.setOrderFrom(OrderFromEnum.MEMBER);
//        query.setOrderType(OrderTypeEnum.NORMAL);
//        query.setAuditStatus(OrderAuditStateEnum.KEFU_AUDITING);
//        IPage<FrontAgentOrderVo> frontAgentOrderVoIPage = agentOrderService.selectAgentOrderListOfPage(initMpPage(), query, abid);
//        return ResultUtil.success(frontAgentOrderVoIPage);
//    }
//
//    /**
//     * 线上返利报表统计
//     *
//     * @apiNote 线上订单 & 正价订单 & 会员下单 & 客服审核通过 & 包含下级订单
//     * @param query 查询条件
//     * @return 统计信息
//     */
//    @PostMapping("profit/statistics")
//    public Result<RebateStatisticVo> profitStatistics(@RequestBody AgentOrderQuery query) {
//        Long abid = AuthUtil.getMemberExt().getAbid();
//        RebateStatisticVo relust = agentOrderService.getFrontAgentprofitStatistics(abid, query);
//        return ResultUtil.success(relust);
//    }


    /**
     * 需收款报表--明细
     *
     * @apiNote 上交人传递 selectedAgentId
     * @param query 查询条件
     * @return 订单列表
     */
    @PostMapping("receive")
    public Result<IPage<FrontAgentOrderVo>> upPay(@RequestBody AgentOrderQuery query) {
        Long auditorID = AuthUtil.getSysExt().getUid();
        query.setAuditor(auditorID);
        query.setIsPay(WhetherEnum.YES);
        query.setAuditDept(AuditDeptEnum.SERVICE_DEPT);
        query.setOrderFrom(OrderFromEnum.AGENT);
        List<Integer> states = Lists.newArrayList();
        states.add(OrderStateEnum.HAS_PAY.getValue());
        states.add(OrderStateEnum.AUDITING.getValue());
        query.setOrderStates(states);
        IPage<FrontAgentOrderVo> record = kefuOrderService.selectUpPayOrderList(initMpPage(), query);
//        IPage<FrontAgentOrderVo> record = agentOrderService.selectUpPayOrderList(initMpPage(), query, agentId);
        return ResultUtil.success(record);
    }


    /**
     * 需收款报表--统计
     *
     * @apiNote 上交人传递 selectedAgentId
     * @param query 查询条件
     * @return 统计信息
     */
    @PostMapping("receive/statistics")
    public Result<UpPayStatisticVo> upPayStatistics(@RequestBody AgentOrderQuery query) {
        Long agentId = AuthUtil.getSysExt().getUid();
        query.setAgentId(agentId);
        UpPayStatisticVo result = kefuOrderService.upPayStatistics(query);
//        UpPayStatisticVo result = orderAgentService.upPayStatistics(query);
        return ResultUtil.success(result);
    }

    /**
     * 退货报表-明细
     *
     * @apiNote 客服审核通过 & 包含下级
     * @param query 查询条件
     * @return 订单列表
     */
    @PostMapping("returnOrder")
    public Result<IPage<FrontAgentOrderVo>> returnOrdersReport(@RequestBody AfterSaleQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setAfterSaleType(AfterSalesTypeEnum.RETURN_GOODS);
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        IPage<FrontAgentOrderVo> iPage = kefuOrderService.selectAfterSaleOrderList(initMPage(), query);
        return ResultUtil.success(iPage);
    }


    /**
     * 退货报表-统计
     *
     * @apiNote 客服审核通过 & 包含下级
     * @param query 查询条件
     * @return 统计信息
     */
    @PostMapping("returnOrder/statistics")
    public Result<AbnormalOrderStatisticVo> returnOrderStatistics(@RequestBody AfterSaleQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setAfterSaleType(AfterSalesTypeEnum.RETURN_GOODS);
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        AbnormalOrderStatisticVo result = kefuOrderService.selectAfterSaleStatistics(query);
        return ResultUtil.success(result);
    }

    /**
     * 换货报表-明细
     *
     * @param query 查询条件
     * @return 订单列表
     */
    @PostMapping("exchangeOrder")
    public Result<IPage<FrontAgentOrderVo>> exchangeOrdersReport(@RequestBody AfterSaleQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setAfterSaleType(AfterSalesTypeEnum.EXCHANE_GOODS);
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        IPage<FrontAgentOrderVo> iPage = kefuOrderService.selectAfterSaleOrderList(initMPage(), query);
        return ResultUtil.success(iPage);
    }

    /**
     * 换货报表-统计
     *
     * @param query 查询条件
     * @return 统计信息
     */
    @PostMapping("exchangeOrder/statistics")
    public Result<AbnormalOrderStatisticVo> exchangeOrdersStatistics(@RequestBody AfterSaleQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setAfterSaleType(AfterSalesTypeEnum.EXCHANE_GOODS);
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        AbnormalOrderStatisticVo result = kefuOrderService.selectAfterSaleStatistics(query);
        return ResultUtil.success(result);
    }


    /**
     * 断货报表-明细
     *
     * @apiNote 客服审核通过 & 包含下级
     * @param query 查询条件
     * @return 订单列表
     */
    @PostMapping("outOfStock")
    public Result<IPage<FrontAgentOrderVo>> outOfStockReport(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setItemStatus(ProductStatusEnum.STATUS6.getValue());
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        IPage<FrontAgentOrderVo> iPage = kefuOrderService.listOrderOfPage(initMPage(), query);
        return ResultUtil.success(iPage);
    }

    /**
     * 断货报表-统计
     *
     * @apiNote 客服审核通过 & 包含下级
     * @param query 查询条件
     * @return 统计信息
     */
    @PostMapping("outOfStock/statistics")
    public Result<AbnormalOrderStatisticVo> outOfStockReportStatistics(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setItemStatus(ProductStatusEnum.STATUS6.getValue());
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        AbnormalOrderStatisticVo result = kefuOrderService.outOfStockReportStatistics(query);
        return ResultUtil.success(result);
    }


    /**
     * 取消报表--明细
     *
     * @param query  查询条件
     * @return 订单列表
     */
    @PostMapping("canceled")
    public Result<IPage<FrontAgentOrderVo>> canceledlist(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setItemStatus(ProductStatusEnum.STATUS7.getValue());
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        IPage<FrontAgentOrderVo> record = kefuOrderService.listOrderOfPage(initMpPage(), query);
        return ResultUtil.success(record);
    }


    /**
     * 取消报表-统计
     *
     * @apiNote 仅支持时间范围查询
     * @param query 查询条件
     * @return 统计信息
     */
    @PostMapping("canceled/statistics")
    public Result<AbnormalOrderStatisticVo> canceledStatistics(@RequestBody AgentOrderQuery query) {
        query.setCustomerId(null == query.getCustomerId() ? AuthUtil.getSysExt().getUid() : query.getCustomerId());
        query.setItemStatus(ProductStatusEnum.STATUS7.getValue());
        if (null != query.getAgentId()){
            AgentRpcVo agentRpcVo = agentManager.loadAgentRpcVo(query.getAgentId());
            AgentOrderUtil.wripeQuery(query,agentRpcVo);
        }
        AbnormalOrderStatisticVo result = kefuOrderService.canceledStatistics(query);
        return ResultUtil.success(result);
    }


    /**
     * 业绩报表
     *
     * @param type 订单类型
     * @param query 查询条件
     * @return 报表明细
     */
    @PostMapping("achievement/{type}")
    public Result<IPage<SalesAchiVo>> achievement(@PathVariable OrderTypeEnum type, @RequestBody AgentOrderQuery query, @SysInfo SysAuth auth) {
        query.setAuditStatus(OrderAuditStateEnum.KEFU_AUDITING);
        query.setCustomerId(null == query.getCustomerId() ? auth.getUid() : query.getCustomerId());
        IPage<SalesAchiVo> page = kefuOrderService.achievement(initMpPage(), type, query);
//        IPage<SalesAchiVo> page = agentOrderService.achievement(initMpPage(), type, query);
        return ResultUtil.success(page);
    }

}
